<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:c="http://java.sun.com/jsp/jstl/core"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:p="http://primefaces.prime.com.tr/ui"
                template="./../template/WorkspaceTemplate.xhtml">

    <ui:define name="top">

    </ui:define>

    <ui:define name="left">
    </ui:define>

    <ui:define name="right">

    </ui:define>

    <ui:define name="content">

        <h2>Sales Price Analysis and Management</h2>

        <h:form id="form" style="font-size: 85%">

            <p:growl id="growl" showDetail="true" life="5000" /> 

            <p:panel>
                <br/>
                <br/>
                <h:outputText value="Price Analysis" style="font-weight: bold; font-size: 110%"/>
                <p:separator/> 
                <br/>
                <p:dataTable id="analysis2" var="rec" value="#{ppManagerBean.priceAnalysis}">

                    <f:facet name="header">
                        <h:outputText value="Wholesales Price Analysis"/>
                    </f:facet>

                    <p:column>
                        <f:facet name="header">
                            Product
                        </f:facet>
                        <h:outputText value="#{rec.product.productType}"/>
                    </p:column>

                    <p:column>
                        <f:facet name="header">
                            Member<br/>Discount
                        </f:facet>
                        <h:outputText value="#{rec.memberDiscount}">
                            <f:convertNumber type="percent"/>
                        </h:outputText>
                    </p:column>

                    <p:column>
                        <f:facet name="header">
                            Order<br/>Discount
                        </f:facet>
                        <h:outputText value="#{rec.orderDiscount}">
                            <f:convertNumber type="percent"/>
                        </h:outputText>
                    </p:column>

                    <p:column>
                        <f:facet name="header">
                            Special<br/>Discount
                        </f:facet>
                        <h:outputText value="#{rec.specialDiscount}">
                            <f:convertNumber type="percent"/>
                        </h:outputText>
                    </p:column>

                    <p:column>
                        <f:facet name="header">
                            Tax Rate
                        </f:facet>
                        <h:outputText value="#{rec.tax}">
                            <f:convertNumber type="percent"/>
                        </h:outputText>
                    </p:column>

                    <p:column>
                        <f:facet name="header">
                            Base Price<br/>(Per Bar)
                        </f:facet>
                        <h:outputText value="#{rec.product.basePrice}">
                            <f:convertNumber type="currency"/>
                        </h:outputText>
                    </p:column>

                    <p:column>
                        <f:facet name="header">
                            Net Price<br/>(Per Bar)
                        </f:facet>
                        <h:outputText value="#{rec.netPriceWS}">
                            <f:convertNumber type="currency"/>
                        </h:outputText>
                    </p:column>

                    <p:column>
                        <f:facet name="header">
                            Profit<br/>(Per Bar)
                        </f:facet>
                        <h:outputText value="#{rec.profitWS}">
                            <f:convertNumber type="currency"/>
                        </h:outputText>
                    </p:column>

                    <p:column>
                        <f:facet name="header">
                            Profit<br/>Margin
                        </f:facet>
                        <h:outputText value="#{rec.profitMarginWS}">
                            <f:convertNumber type="percent"/>
                        </h:outputText>
                    </p:column>
                </p:dataTable>
                <br/>
                <br/>
                
                <p:dataTable id="analysis" var="rec" value="#{ppManagerBean.priceAnalysis}">

                    <f:facet name="header">
                        <h:outputText value="Direct Sales Price Analysis"/>
                    </f:facet>

                    <p:column>
                        <f:facet name="header">
                            Product
                        </f:facet>
                        <h:outputText value="#{rec.product.productType}"/>
                    </p:column>

                    <p:column>
                        <f:facet name="header">
                            Member<br/>Discount
                        </f:facet>
                        <h:outputText value="#{rec.memberDiscount}">
                            <f:convertNumber type="percent"/>
                        </h:outputText>
                    </p:column>

                    <p:column>
                        <f:facet name="header">
                            Order<br/>Discount
                        </f:facet>
                        <h:outputText value="#{rec.orderDiscount}">
                            <f:convertNumber type="percent"/>
                        </h:outputText>
                    </p:column>

                    <p:column>
                        <f:facet name="header">
                            Special<br/>Discount
                        </f:facet>
                        <h:outputText value="#{rec.specialDiscount}">
                            <f:convertNumber type="percent"/>
                        </h:outputText>
                    </p:column>

                    <p:column>
                        <f:facet name="header">
                            Tax Rate
                        </f:facet>
                        <h:outputText value="#{rec.tax}">
                            <f:convertNumber type="percent"/>
                        </h:outputText>
                    </p:column>

                    <p:column>
                        <f:facet name="header">
                            Base Price<br/>(Per Bar)
                        </f:facet>
                        <h:outputText value="#{rec.product.basePrice}">
                            <f:convertNumber type="currency"/>
                        </h:outputText>
                    </p:column>

                    <p:column>
                        <f:facet name="header">
                            Net Price<br/>(Per Bar)
                        </f:facet>
                        <h:outputText value="#{rec.netPriceDS}">
                            <f:convertNumber type="currency"/>
                        </h:outputText>
                    </p:column>

                    <p:column>
                        <f:facet name="header">
                            Profit<br/>(Per Bar)
                        </f:facet>
                        <h:outputText value="#{rec.profitDS}">
                            <f:convertNumber type="currency"/>
                        </h:outputText>
                    </p:column>

                    <p:column>
                        <f:facet name="header">
                            Profit<br/>Margin
                        </f:facet>
                        <h:outputText value="#{rec.profitMarginDS}">
                            <f:convertNumber type="percent"/>
                        </h:outputText>
                    </p:column>
                </p:dataTable>
                <br/>
                <br/>
                <h:panelGrid columns="3" cellpadding="10">  
                    <h:panelGroup>  
                        <h:outputText style="font-weight: bold" value="Member Discount(%): "/>  
                        <h:outputText id="output1" value="#{ppManagerBean.memberDiscount}"/>  
                    </h:panelGroup>
                    <h:inputHidden id="member" value="#{ppManagerBean.memberDiscount}"/>  
                    <p:slider for="member" display="output1" style="width:200px" minValue="#{ppManagerBean.minMemberDiscount}" maxValue="#{ppManagerBean.maxMemberDiscount}" onSlideEndUpdate="form:analysis form:analysis2" slideEndListener="#{ppManagerBean.onSlideEnd}"/>

                    <h:panelGroup>  
                        <h:outputText style="font-weight: bold" value="Order Discount(%): "/>  
                        <h:outputText id="output2" value="#{ppManagerBean.orderDiscount}"/>  
                    </h:panelGroup>
                    <h:inputHidden id="order" value="#{ppManagerBean.orderDiscount}"/>  
                    <p:slider for="order" display="output2" style="width:200px" minValue="0" maxValue="#{ppManagerBean.maxOrderDiscount}" onSlideEndUpdate="form:analysis form:analysis2" slideEndListener="#{ppManagerBean.onSlideEnd}"/>

                    <h:panelGroup>  
                        <h:outputText style="font-weight: bold" value="Special Discount(%): "/>  
                        <h:outputText id="output3" value="#{ppManagerBean.specialDiscount}"/>  
                    </h:panelGroup>
                    <h:inputHidden id="special" value="#{ppManagerBean.specialDiscount}"/>  
                    <p:slider for="special" display="output3" style="width:200px" minValue="0" maxValue="#{ppManagerBean.specialDiscountRange.doubleValue.doubleValue()*100}" onSlideEndUpdate="form:analysis form:analysis2" slideEndListener="#{ppManagerBean.onSlideEnd}"/>
                </h:panelGrid>


                <br/>
                <br/>                
                <br/>
                <br/>
                <br/>
                <h:outputText value="Manage Product Price" style="font-weight: bold; font-size: 110%"/>
                <p:separator/> 
                <br/>

                <p:dataTable id ="productPrices" var="product" value="#{ppManagerBean.viewAllProductPrices()}" style="border: black" 

                             selection="#{ppManagerBean.selectedProduct}" selectionMode="single" emptyMessage="No products found with given criteria" >  

                    <f:facet name="header">  
                        Click on "Update"  after selecting a row to update price details  
                    </f:facet>  

                    <p:column filterBy="#{product.productType}"   
                              headerText="Product Type" 
                              filterOptions="#{ppManagerBean.createFilterOptions()}" 
                              filterMatchMode="exact">  
                        <h:outputText value="#{product.productType}" />  
                    </p:column>  


                    <p:column   headerText="Base Price"  >  
                        <h:outputText value="#{product.basePrice}"  > 
                            <f:convertNumber maxFractionDigits="2" minFractionDigits="2"/>
                        </h:outputText>
                    </p:column>  

                    <p:column  headerText="Markup(W)" >  
                        <h:outputText value="#{product.wsMarkup}" >  
                            <f:convertNumber type="percent" />
                        </h:outputText>
                    </p:column>  

                    <p:column 
                        headerText="Unit Price(W)"  >  
                        <h:outputText value="#{product.wsUnitPrice}" >  
                            <f:convertNumber maxFractionDigits="2" minFractionDigits="2" />
                        </h:outputText>
                    </p:column>  

                    <p:column  
                        headerText="Markup(D)" >  
                        <h:outputText value="#{product.dsMarkup}" > 
                            <f:convertNumber type="percent" />
                        </h:outputText>
                    </p:column>  

                    <p:column 
                        headerText="Unit Price(D)"  >  
                        <h:outputText value="#{product.dsUnitPrice}" >  
                            <f:convertNumber maxFractionDigits="2" minFractionDigits="2" />
                        </h:outputText>
                    </p:column> 

                    <f:facet name="footer">  
                        <p:commandButton value="Update" image="ui-icon ui-icon-pencil"  
                                         update=":dialogForm:display" oncomplete="updateDlg.show()"/>
                    </f:facet>
                </p:dataTable>  

                <br/>
                <br/>
                <br/>
                <h:outputText value="Manage Rates" style="font-weight: bold; font-size: 110%"/>
                <p:separator/> 
                <br/>
                <h:panelGrid columns="2" cellpadding="10">
                    <h:outputLabel style="font-weight: bold" value="Tax rate: "/>
                    <p:inputText value="#{ppManagerBean.tax.doubleValue}"/>
                    <h:outputLabel style="font-weight: bold" value="Maximum Special Discount Rate: "/>
                    <p:inputText value="#{ppManagerBean.specialDiscountRange.doubleValue}"/>
                </h:panelGrid>
                <br/>
                <br/>
                <div align="right">
                    <p:commandButton image="ui-icon ui-icon-pencil" value="Update Rates" actionListener="#{ppManagerBean.updateRates}" update="form"/>
                </div>
                <br/>
                <br/>
            </p:panel>

            <br/>
            <br/>

        </h:form> 



        <p:dialog id="updateDialogue" header="Update Product Price" widgetVar="updateDlg" resizable="false" modal="true"  
                  width="400" showEffect="clip" hideEffect="fold">  

            <h:form id="dialogForm">

                <h:panelGrid id="display" columns="2" cellpadding="4">  

                    <h:outputText value="Product Type:" />
                    <h:outputText value="#{ppManagerBean.selectedProduct.productType}" />  

                    <h:outputText value="Whole Sales Markup:" />   
                    <p:inputText id="wsMarkup" required="true" requiredMessage="Whole Sales Markup is required." 
                                 label ="wsMarkup" value="#{ppManagerBean.selectedProduct.wsMarkup}" />

                    <h:outputText value="Direct Sales Markup:" />   
                    <p:inputText id="dsMarkup" required="true" requiredMessage="Direct Sales Markup is required."
                                 label ="dsMarkup" value="#{ppManagerBean.selectedProduct.dsMarkup}" />

                </h:panelGrid>  
                <p:commandButton value="Submit" actionListener="#{ppManagerBean.updateProductPrice}" update="form" oncomplete="handleLoginRequest(xhr, status, args)"/>  

            </h:form>  
        </p:dialog>

        <script type="text/javascript">  
            function handleLoginRequest(xhr, status, args) {  
                 if(args.validationFailed || !args.valid) {  
                 jQuery('#updateDialogue').parent().effect("shake", { times:3 }, 100);  
                 } else {  
                     updateDlg.hide();  
                    //jQuery('#updateDlg').fadeOut();  
                }  
            }  
        </script>  
        <!--
        <p:confirmDialog message="Are you sure?" width="200"  
                         showEffect="explode" hideEffect="explode"  
                         header="Confirm" severity="alert" widgetVar="confirmation">  
            <h:form>
            <p:commandButton value="Yes" update="form" actionListener="{ppManagerBean.deleteProductPrice}" oncomplete="confirmation.hide()"/>  
            <p:commandButton value="Cancel" onclick="confirmation.hide()" type="button" />  
            </h:form>
        </p:confirmDialog> 
        -->

    </ui:define>

    <ui:define name="bottom">

    </ui:define>

</ui:composition>
